//
//  FuJinBigCollectionViewCell.m
//  GOGOTravel
//
//  Created by dllo on 16/3/3.
//  Copyright © 2016年 果果. All rights reserved.
//

#import "FuJinBigCollectionViewCell.h"
#import "FuJin.h"
#import "FuJinTableViewCell.h"
@interface FuJinBigCollectionViewCell ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArr;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, assign) BOOL isHeaderRef;
@end

@implementation FuJinBigCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self createTabelView];
        

        
        [self addFooterRef];
        [self addHeaderRed];
        
        self.dataArr = [NSMutableArray array];
        
        _page = 0;
    }
    return self;
}

- (void)dealloc{
    [_tableView release];
    [_dataArr release];
    [_location release];
    [super dealloc];
}

- (void)addHeaderRed{
    
    [_tableView addHeaderWithCallback:^{
       
        _page = 0;
        
        
        [self handelData];
        _isHeaderRef = YES;
        
    }];
    
    
}



- (void)addFooterRef{
    
    [_tableView addFooterWithCallback:^{
       _page = _page + 20;
        [self handelData];
        
        _isHeaderRef = NO;
    }];
}

- (void)createTabelView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, self.frame.size.width, self.frame.size.height)];
    
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:_tableView];
    
    [_tableView release];
    
    [_tableView registerClass:[FuJinTableViewCell class] forCellReuseIdentifier:@"FuJinTableViewCell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FuJinTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"FuJinTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    FuJin *model = _dataArr[indexPath.row];
    cell.model = model;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    FuJin *model = _dataArr[indexPath.row];
    
   
    [self.delegate jumpToVc:model];
    
    
}


- (void)setUrl:(NSInteger)url {
    _url = url;
    
    [_dataArr removeAllObjects];
    
    [self handelData];
    
    
    
    [_tableView reloadData];
}


- (void)setLocation:(NSDictionary *)location{
    if (_location != location) {
        [_location release];
        _location = [location retain];
    }
    
}


- (void)handelData{
    NSLog(@"dddddddddd%ld", (long)_url);
    
    NSNumber *lat = _location[@"lat"];
    NSNumber *lng = _location[@"lng"];
    
    //latitude=38.883521&longitude=121.544549
    
    
    
    
    
    NSString *resultUrl = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=%ld&start=%ld&count=20&latitude=%@&longitude=%@&sign=097db75ba7c494f6e7f1f5959a99be46", (long)_url, _page, lat, lng];
    
    
   
    
    [AFNetWorkingHandle GETRequestWithURLString:resultUrl completionWithSuccess:^(id result) {
        
        NSDictionary *dic = result;
     
        NSArray *arr = dic[@"items"];
        
        
        
        if (_isHeaderRef == YES) {
            [_dataArr removeAllObjects];
        }
        
        NSArray *arrLinShi = [NSArray array];
        
       arrLinShi = [FuJin ModelHandleWithArray:arr];
        
        for (FuJin *model in arrLinShi) {
            [_dataArr addObject:model];
        }
        
        
        
        
         NSLog(@"%@", resultUrl);
        [_tableView reloadData];
        
        
        
        
        [_tableView footerEndRefreshing];
        
        [_tableView headerEndRefreshing];
    } withFailure:^(id error) {
        
    }];
    
    
    
}







@end
